package com.fuhe.chen.vendingmachine.controller.admin;

import com.alibaba.fastjson.JSON;
import com.fuhe.chen.vendingmachine.anno.AddLog;
import com.fuhe.chen.vendingmachine.pojo.Category;
import com.fuhe.chen.vendingmachine.service.ICategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@Api(tags = "商品分类管理")
@RequestMapping("/admin/category")
public class CategoryController {

    @Autowired
    ICategoryService categoryService ;


    @ApiOperation(value = "获取全部商品分类", notes="返回json格式数据")
    @GetMapping({""})
    @ResponseBody
    public String getCategories(){
        List<Category> all = categoryService.findAll();
        return JSON.toJSONString(all);
    }

    @ApiOperation(value = "添加商品分类", notes="返回添加结果")
    @PostMapping({"/addCategory"})
    @ResponseBody
    @AddLog(operation = "添加商品分类")
    public String addCategory(String categoryName){
        String msg = categoryService.addCategory(categoryName.trim());

        return msg;
    }

    @ApiOperation(value = "删除商品分类", notes="返回删除结果")
    @PostMapping({"/delCategory"})
    @ResponseBody
    @AddLog(operation = "删除商品分类")
    public String delCategory(Integer id){
        categoryService.deleteCategory(id);

        return "删除成功";
    }

}
